<?php
/**
 * EZ
 * Copyright (c) 2017 http://www.lienze.com All rights reserved.
 * author: EZ <123400558@qq.com>
 * Date: 2017/6/15  Time: 14:52
 */

namespace Admin\Model;


use Think\Model;

class MemoModel extends Model
{
    /**
     * 获取留言文章列表
     * @param string $field     获取字段
     * @param null   $where     查询条件
     * @param int    $start     limit第一参数
     * @param int    $rows      limitt第二参数
     * @param string $sortField 排序字段
     * @param string $sortType  排序方式
     * @param bool   $total     是否获取文章列表总数
     * @return mixed
     */
    public function getInfo($field = '*', $where = null, $start = 0, $rows = 5, $sortField = 'id', $sortType = 'desc', $total = false)
    {
        if(empty($start)) $start         = 0;
        if(empty($rows)) $rows           = '5';
        if(empty($sortField)) $sortField = 'id';
        if(empty($sortType)) $$sortType  = 'desc';


        $data = $this->field($field)->where($where)->order("{$sortField} {$sortType}")->limit($start, $rows)->select();

        if($total) {
            $sumList = $this->getListSum($field, $where, $sortField, $sortType);
            $data['total'] = $sumList;
        }

        return $data;
    }

    /**
     * 获取留言文章列表数量
     * @param string $field     获取字段
     * @param null   $where     查询条件
     * @param string $sortField 排序字段
     * @param string $sortType  排序方式
     * @return mixed
     */
    public function getListSum($field = '*', $where = null, $sortField = 'id', $sortType = 'desc')
    {
        if(empty($sortField)) $sortField = 'id';
        if(empty($sortType)) $$sortType  = 'desc';

        $sum = $this->field($field)->where($where)->order("{$sortField} {$sortType}")->count();
        return $sum;
    }

}